#!/usr/bin/env python
# -- coding=utf-8 --
from tornado import web, httpserver, ioloop

from tornado import options
import json

options.options.define("port", 8000, type=int, help="启动server")


class IndexHandler(web.RequestHandler):
    def get(self):
        itcast = {
            "name": "小米",
            "age": 6,
            "job": "手机"
        }

        json_str = json.dumps(itcast)
        # self.set_header("Content-Type", "application/json")
        self.set_header("itcast", "python")
        # self.set_status(464,"python no work")
        self.write(json_str)
        self.redirect("http://192.168.0.62:8080/admin/weekly")
        # self.write(itcast)

    def set_default_headers(self):
        self.set_header("Content-Type", "application/json")


class ErrorHandler(web.RequestHandler):
    def get(self):
        self.write("hello")
        self.send_error(status_code=500, title="服务器错误", content="服务器运行发生错误，服务访问")

    def write_error(self, status_code, **kwargs):
        self.write("<h1>title:%s</h1>" % kwargs.get("title"))
        self.write("<h1>Content:%s</h1>" % kwargs.get("content"))


def main():
    options.parse_command_line()
    app = web.Application([
        (r"^/$", IndexHandler),
        (r"^/senderror$", ErrorHandler)
    ])
    http_server = httpserver.HTTPServer(app)
    http_server.listen(options.options.port)
    ioloop.IOLoop.current().start()


if __name__ == '__main__':
    main()
